package com.Practice;

import java.io.IOException;
import java.net.*;
import java.util.Scanner;

public class SockedTest {
    public static void main(String[] args) {
        System.out.println("客户端启动");
        try (DatagramSocket socket = new DatagramSocket(7788);) {
            Scanner sc = new Scanner(System.in);
            while (true) {
                System.out.println("你说：");
                String string = sc.nextLine();
                if ("exit".equals(string)) {
                    System.out.println("关闭客服端");
                    break;
                }
                InetAddress address =InetAddress.getByAddress(new byte[]{10,0,3,-48});
                byte[] bytes = string.getBytes("UTF-8");
                DatagramPacket packet = new DatagramPacket(bytes,0, bytes.length, address, 8888);
                socket.send(packet);

                //接收服务端信息
                DatagramPacket packet1 = new DatagramPacket(bytes,0, bytes.length,packet.getAddress(), packet.getPort());
                socket.receive(packet1);
                String receiveString = new String(packet1.getData(),0, packet1.getLength(),"UTF-8");
                System.out.println("服务端回复了：" + receiveString);
                }

            } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
